package com.gzs.learn.payment.pay.consumer;

import cn.felord.payment.wechat.v3.model.PromotionDetail;
import cn.felord.payment.wechat.v3.model.TransactionConsumeData;
import com.google.common.collect.Lists;
import com.gzs.learn.common.util.JsonUtil;
import com.gzs.learn.payment.inf.enums.PaymentStatusEnum;
import com.gzs.learn.payment.model.po.PaySuccess;
import com.gzs.learn.payment.model.po.PaySuccessWechat;
import com.gzs.learn.payment.model.po.PaySuccessWechatPromotion;
import com.gzs.learn.payment.service.IPaymentService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * 微信支付回调通知处理逻辑
 *
 * @author guanzhisong
 * @date 2021-08-30
 */
@Component
@RequiredArgsConstructor
public class WechatTransactionCallbackConsumer implements Consumer<TransactionConsumeData> {
    private final IPaymentService paymentService;

    @Override
    public void accept(TransactionConsumeData data) {
        PaySuccess paySuccess = convertPaySuccess(data);
        PaySuccessWechat paySuccessWechat = convertPaySuccessWechat(data);
        List<PaySuccessWechatPromotion> wechatPromotions = convertWechatPromotion(data);
        paySuccessWechat.setPromotions(wechatPromotions);
        // 微信支付回调
        paymentService.callback(paySuccess, paySuccessWechat);
    }

    private PaySuccess convertPaySuccess(TransactionConsumeData data) {
        PaySuccess paySuccess = new PaySuccess();
        paySuccess.setSerial(Long.valueOf(data.getOutTradeNo()));
        paySuccess.setSerialThird(data.getTransactionId());
        paySuccess.setAmt(data.getAmount().getTotal().longValue());
        paySuccess.setStatus(PaymentStatusEnum.S.getCode());
        paySuccess.setStatusThird(data.getTradeState().toString());
        paySuccess.setCreateTime(new Date());
        paySuccess.setUpdateTime(new Date());
        return paySuccess;
    }

    private PaySuccessWechat convertPaySuccessWechat(TransactionConsumeData data) {
        PaySuccessWechat paySuccessWechat = new PaySuccessWechat();
        paySuccessWechat.setAttach(data.getAttach());
        paySuccessWechat.setBankType(data.getBankType());
        paySuccessWechat.setCreateTime(new Date());
        paySuccessWechat.setCurrency(data.getAmount().getCurrency());
        paySuccessWechat.setDeviceId(data.getSceneInfo().getDeviceId());
        paySuccessWechat.setNonceStr("");
        paySuccessWechat.setOpenid(data.getPayer().getOpenid());
        paySuccessWechat.setPayerCurrency(data.getAmount().getPayerCurrency());
        paySuccessWechat.setPayerTotal(data.getAmount().getPayerTotal().longValue());
        paySuccessWechat.setSerial(Long.valueOf(data.getOutTradeNo()));
        paySuccessWechat.setSerialWechat(data.getTransactionId());
        // utc+8北京时区
        paySuccessWechat.setSuccessTime(Date.from(data.getSuccessTime().toInstant(ZoneOffset.of("+08:00"))));
        paySuccessWechat.setTotal(data.getAmount().getTotal().longValue());
        paySuccessWechat.setTradeState(data.getTradeState().toString());
        paySuccessWechat.setTradeType(data.getTradeType().toString());
        return paySuccessWechat;
    }

    private List<PaySuccessWechatPromotion> convertWechatPromotion(TransactionConsumeData data) {
        if (CollectionUtils.isEmpty(data.getPromotionDetail())) {
            return Lists.newArrayList();
        }
        List<PaySuccessWechatPromotion> promotions = Lists.newArrayList();
        for (PromotionDetail detail : data.getPromotionDetail()) {
            PaySuccessWechatPromotion promotion = new PaySuccessWechatPromotion();
            promotion.setAmount(detail.getAmount());
            promotion.setCouponId(detail.getCouponId());
            promotion.setCreateTime(new Date());
            promotion.setCurrency(detail.getCurrency());
            promotion.setGoodsDetail(StringUtils.left(JsonUtil.toJSONString(detail.getGoodsDetail()), 1000));
            promotion.setMerchantContribute(detail.getMerchantContribute());
            promotion.setName(detail.getName());
            promotion.setOtherContribute(detail.getOtherContribute());
            promotion.setScope(detail.getScope());
            promotion.setSerial(Long.valueOf(data.getOutTradeNo()));
            promotion.setStockId(detail.getStockId());
            promotion.setType(detail.getType());
            promotion.setWechatpayContribute(detail.getWechatpayContribute());
            promotion.setWechatSerial(data.getTransactionId());

            promotions.add(promotion);
        }
        return promotions;
    }
}
